package cn.mdmbct.douyin.spring.example.service;

import cn.mdmbct.douyin.basis.body.video.AutoCreateVideoParam;
import cn.mdmbct.douyin.basis.response.video.VideoCreateRes;
import cn.mdmbct.douyin.basis.service.interfaces.DyBasisService;
import cn.mdmbct.douyin.basis.service.interfaces.VideoManagerService;
import cn.mdmbct.douyin.common.enums.ApiPlatform;
import cn.mdmbct.douyin.spring.example.dto.DyVideoPublishDto;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * 抖音视频推送 异步
 *
 * @author mdmbct  mdmbct@outlook.com
 * @date 2021/3/18 9:20
 * @modified mdmbct
 * @since 1.0
 */
@Service
@EnableAsync
public class DyVideoPushService {


    @Resource
    private DyBasisService dyBasisService;

    @Async
    public void pushVideo(DyVideoPublishDto dto) {

        // 检查文件是否存在
        File videoFile = new File(dto.getVideoFilePath());
        if (!videoFile.exists()) {
            System.out.println("选择的文件不存在");
        }

        VideoManagerService dyVideoService = dyBasisService.getVideoService();
        // 参数准备
        StringBuilder stringBuilder = new StringBuilder(dto.getTitle());
        if (dto.getTopic() != null && dto.getTopic().length != 0) {
            for (String s : dto.getTopic()) {
                stringBuilder.append(s).append(' ');
            }
        }
        if (dto.getAt() != null && dto.getAt().length != 0) {
            for (String s : dto.getAt()) {
                stringBuilder.append('@').append(s).append(' ');
            }
        }

        AutoCreateVideoParam param = AutoCreateVideoParam.builder()
                .atUsers(dto.getAt())
                .coverTsp(dto.getCoverTsp())
                .text(stringBuilder.toString())
                .build();
        VideoCreateRes.VideoCreateResData videoCreateResData = null;
        try {
            videoCreateResData = dyVideoService.autoCreateVideo(dto.getOpenId(), videoFile, param, ApiPlatform.DOU_YIN);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("选择的文件不存在");
        }
        System.out.println(videoCreateResData.getItemId());
    }


}
